﻿using System.IO.Compression;

namespace K9Nano.Updater.Utils;

internal static class ZipUtils
{
    public static void CreateZipFromDirectory(string sourceDirectory, string destinationZipPath)
    {
        if (!Directory.Exists(sourceDirectory))
        {
            throw new UpdaterException($"目录不存在: {sourceDirectory}");
        }

        // 确保目标 ZIP 文件路径合法
        if (File.Exists(destinationZipPath))
        {
            File.Delete(destinationZipPath); // 覆盖已存在的 ZIP 文件
        }

        // 创建 ZIP 文件（自动包含子目录）
        ZipFile.CreateFromDirectory(
            sourceDirectoryName: sourceDirectory,
            destinationArchiveFileName: destinationZipPath,
            compressionLevel: CompressionLevel.Optimal,
            includeBaseDirectory: false // 是否包含根目录本身
        );
    }

    public static void ExtractToDirectory(string sourceZipPath, string destinationDirectory)
    {
        if (!File.Exists(sourceZipPath))
        {
            throw new UpdaterException($"待解压的原文件不存在: {sourceZipPath}");
        }

        ZipFile.ExtractToDirectory(sourceZipPath, destinationDirectory, true);
    }
}
